package com.coffee.admin.service.platform.impl;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.platform.query.PlatformSystemDeployQuery;
import com.coffee.admin.api.platform.result.PlatformSystemDeployResult;
import com.coffee.admin.api.platform.service.PlatformSystemDeployService;
import com.coffee.admin.service.platform.bean.PlatformSystemDeploy;
import com.coffee.admin.service.platform.dao.PlatformSystemDeployDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class PlatformSystemDeployServiceImpl implements PlatformSystemDeployService {
    private final static Logger logger = LoggerFactory.getLogger(PlatformSystemDeployServiceImpl.class);
    @Resource
    private PlatformSystemDeployDao platformSystemDeployDao;

    @Override
    public Page<PlatformSystemDeployResult> getPager(PlatformSystemDeployQuery param, PagerInfo pr) {
        Page<PlatformSystemDeployResult> pager = new Page<PlatformSystemDeployResult>(pr.getStart(), pr.getPageSize());
        Integer count = platformSystemDeployDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<PlatformSystemDeploy> list = platformSystemDeployDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, PlatformSystemDeployResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(PlatformSystemDeployQuery param) {
        Integer count = platformSystemDeployDao.count(param);
        return count;
    }

    @Override
    public PlatformSystemDeployResult getDetail(Integer id) {
        PlatformSystemDeploy item = platformSystemDeployDao.findById(id);
        if (item == null) {
            return null;
        }
        PlatformSystemDeployResult result = new PlatformSystemDeployResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<PlatformSystemDeployResult> getList(PlatformSystemDeployQuery param, PagerInfo pr) {
        List<PlatformSystemDeploy> list = platformSystemDeployDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, PlatformSystemDeployResult.class);
    }

    @Override
    public List<PlatformSystemDeployResult> getList(PlatformSystemDeployQuery param) {
        List<PlatformSystemDeploy> list = platformSystemDeployDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, PlatformSystemDeployResult.class);
    }

    @Override
    public List<PlatformSystemDeployResult> getList() {
        PlatformSystemDeployQuery param = new PlatformSystemDeployQuery();
        List<PlatformSystemDeploy> list = platformSystemDeployDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, PlatformSystemDeployResult.class);
    }


    @Override
    public void delete(Integer id) {
        platformSystemDeployDao.deleteById(id);
    }

    @Override
    public void addOrUpdate(PlatformSystemDeployResult result) throws BusinessException {
        logger.info("result===" + JSONObject.toJSONString(result));
        PlatformSystemDeploy item = new PlatformSystemDeploy();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            platformSystemDeployDao.insert(item);
        } else {
            PlatformSystemDeploy tmp = platformSystemDeployDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            platformSystemDeployDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, PlatformSystemDeployResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, PlatformSystemDeployResult> map = new HashMap<Integer, PlatformSystemDeployResult>();
        List<PlatformSystemDeploy> list = platformSystemDeployDao.findByIds(StringUtil.setToList(idSet));
        List<PlatformSystemDeployResult> resultList = DTOUtils.convertList(list, PlatformSystemDeployResult.class);
        for (PlatformSystemDeployResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, PlatformSystemDeployResult> getItemMap() {
        Map<Integer, PlatformSystemDeployResult> map = new HashMap<Integer, PlatformSystemDeployResult>();
        PlatformSystemDeployQuery param = new PlatformSystemDeployQuery();
        List<PlatformSystemDeploy> list = platformSystemDeployDao.page(param, 0, Integer.MAX_VALUE);
        List<PlatformSystemDeployResult> resultList = DTOUtils.convertList(list, PlatformSystemDeployResult.class);
        for (PlatformSystemDeployResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }


}
